#include "in.h"

uint32_t Hash( unsigned char *str , size_t len )
{
	unsigned char		*p = str ;
	unsigned char		*p_end = str + len ;
	register uint32_t	ha = 0 ;
	
	for( ; p < p_end ; p++ )
	{
		ha  = (*p) + (ha<<6) + (ha>>16) - ha ;
	}
	
	return ha;
}

void printhex( char *buf , size_t buf_len )
{
	register size_t		i ;
	
	printf( "[" );
	for( i = 0 ; i < buf_len ; i++ )
	{
		printf( "0x%02X" , (unsigned char)(buf[i]) );
		if( i + 1 < buf_len )
			printf( " " );
	}
	printf( "]" );
	
	return;
}

int ConvertArgsToArray( va_list valist , struct MemField *field_array , size_t field_array_size , size_t *p_field_array_count )
{
	char		*field_name = NULL ;
	size_t		field_array_count ;
	
	memset( field_array , 0x00 , sizeof(struct MemField)*field_array_size );
	field_array_count = 0 ;
	while(1)
	{
		if( field_array_count >= field_array_size )
			return (tls_last_errno=MDB_ERROR_TOO_MANY_FIELDS);
		
		field_name = va_arg( valist , char* ) ;
		if( field_name == MDBFIELD_NONE )
			break;
		field_array[field_array_count].field_name = field_name ;
		field_array[field_array_count].field_offset = va_arg( valist , int ) ;
		field_array[field_array_count].field_size = va_arg( valist , size_t ) ;
		TRACE_PRINTF( "convert arg .field_name[%s] .field_offset[%d] .field_size[%zu]\n" , field_array[field_array_count].field_name , field_array[field_array_count].field_offset , field_array[field_array_count].field_size );
		field_array_count++;
	}
	
	TRACE_PRINTF( "convert field_array_count[%zu]\n" , field_array_count );
	if( p_field_array_count )
		(*p_field_array_count) = field_array_count ;
	
	return 0;
}

